package dk.dashboardFP.panels.displayPanel {
	import flash.utils.Dictionary;

	import dk.dashboardFP.dfpLogger.DFPLogger;
	import dk.dashboardFP.panels.displayPanel.core.DisplayEditorCoreEvent;
	import dk.dashboardFP.panels.displayPanel.marks.PointerMark;

	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;

	/**
	 * @author josef
	 */
	public class DisplayOverlayHandler extends EventDispatcher {
		private var _oDisplayPanel : DisplayPanel;
		private var _sprPointMarkContainer : Sprite;
		private var _doStage : Stage;
		private var _doCurrentMarked : DisplayObject;
		private var _doPointerMark : PointerMark;
		private var _sprSelectMarkContainer : Sprite;
		private var _oSelctionPointsDict : Dictionary;

		public function DisplayOverlayHandler(oDisplayPanel : DisplayPanel) {
			_oDisplayPanel = oDisplayPanel;
			super();
			
			setup();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setup() : void {
			
			//:::::::
			// stage 
			_doStage = _oDisplayPanel.doDashboard.doStage;
			
			//:::::::
			// container 
			_sprSelectMarkContainer = new Sprite();
			_oDisplayPanel.doDashboard.sprOverlayHolder.addChild(_sprSelectMarkContainer);

			_sprPointMarkContainer = new Sprite();
			_oDisplayPanel.doDashboard.sprOverlayHolder.addChild(_sprPointMarkContainer);
			
			//:::::::
			// register editor core 
			_oDisplayPanel.oDisplayEditorCore.addEventListener(DisplayEditorCoreEvent.ITEM_SELCETED, onItemSelected, false, 0, true);
			_oDisplayPanel.oDisplayEditorCore.addEventListener(DisplayEditorCoreEvent.ITEM_DESELCETED, onItemDeSelected, false, 0, true);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// selection
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		//:::::::
		// core handlers

		private function onItemDeSelected(event : DisplayEditorCoreEvent) : void {
			if (event.oEI.doSelectionMark) {	
				if (_sprSelectMarkContainer.contains(event.oEI.doSelectionMark)) {
					_sprSelectMarkContainer.removeChild(event.oEI.doSelectionMark);
				}
			}
		}

		private function onItemSelected(event : DisplayEditorCoreEvent) : void {
			var doSelectionMark : SelectionMark = new SelectionMark(event.oEI, _sprSelectMarkContainer);
			_sprSelectMarkContainer.addChild(doSelectionMark);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// interface
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		//:::::::
		// mark 
		public function markPointerObject(doMarked : DisplayObject) : void {
			doMarkPointerObject(doMarked);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// mark object
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function doMarkPointerObject(doMarked : DisplayObject) : void {
			
			
			//:::::::
			// clear old 
			if (_doCurrentMarked) {
				if (_sprPointMarkContainer.contains(_doPointerMark)) {
					_sprPointMarkContainer.removeChild(_doPointerMark);
				}
			}
			
			//:::::::
			// clear if null 
			if (!doMarked) {	
				while(_sprPointMarkContainer.numChildren) {
					_sprPointMarkContainer.removeChildAt(0);
				}

				_doCurrentMarked = null;
				return;
			}
			
			_doCurrentMarked = doMarked;
			
			//:::::::
			// set mark
			if (_doCurrentMarked) {
				_doPointerMark = new PointerMark(_doCurrentMarked, _sprPointMarkContainer);
				_sprPointMarkContainer.addChild(_doPointerMark);
			}
		}
	}
}
